package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.AuditClaim;
import com.xmy.cultivate.entity.AuditReturnPremium;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 退费申请 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-04-22
 */
public interface AuditReturnPremiumMapper extends BaseMapper<AuditReturnPremium> {

    @Select({"<script>",
            "SELECT ",
            "t1.*, ",
            "getDictName(t1.year_class_id, 14) as year_class_name,",
            "t2.`name` as student_name, ",
            "t3.content, ",
            "getSubjectsName( t1.subjects_id ) as subjects_name, ",
            "t4.arrive_step, ",
            "t4.version_num, ",
            "t4.reason,",
            "t7.name as teacher_name",
            "FROM ",
            "audit_return_premium AS t1 ",
            "LEFT JOIN student AS t2 ON t1.student_id = t2.id ",
            "LEFT JOIN common_cause AS t3 ON t1.cause_id = t3.id AND t3.type = 1 AND t3.deleted = 0 ",
            "LEFT JOIN audit_claim AS t4 ON t1.id = t4.id",
            "LEFT JOIN audit_flow_detail t5 ON t1.id = t5.sourse_id ",
            "LEFT JOIN grade t6 ON t1.grade_id = t6.id ",
            "LEFT JOIN staff t7 ON t6.teacher_id = t7.id ",
            "WHERE 1=1",
            "AND t1.`status` &lt;&gt; 3 ",
            "<when test='adminId!=0'>",
                "AND t5.check_id = ${adminId}",
            "</when>",

            "<when test='status!=null and status!=&apos;&apos; '>",
                "<when test='status == 0'>",
                // 待处理
                    "<when test='adminId==0'>",
                        "AND t4.`status` = 0",
                    "</when>",
                    "AND (( t4.arrive_step &gt;= t5.step_num AND t5.type = 1 AND t4.`status` = 0 AND t5.`status` = 0 ) OR ( t5.type = 2 AND  t5.is_arrive = 1))",
                    "<when test='startDate!=null and startDate!=&apos;&apos; and endDate!=null and endDate!=&apos;&apos;'>",
                        "AND t4.created_at BETWEEN #{startDate} AND #{endDate} ",
                    "</when>",
                "</when>",

                "<when test='status == 1'>",
                // 已处理
                    "<when test='adminId==0'>",
                        "AND t4.`status` &lt;&gt; 0",
                    "</when>",
                    "AND t5.`status` &lt;&gt; 0 AND t5.type = 1",
                    "<when test='startDate!=null and startDate!=&apos;&apos; and endDate!=null and endDate!=&apos;&apos;'>",
                        "AND t5.operate_date_time BETWEEN #{startDate} AND #{endDate} ",
                    "</when>",
                "</when>",
            "</when>",

            "GROUP BY t1.id ",
            "ORDER BY t1.id DESC ",
            "</script>"})
    IPage<AuditReturnPremium> getAuditReturnPremiumList(IPage<?> page,
                                                        @Param("yearPart") String yearPart,
                                                        @Param("quarterNum") String quarterNum,
                                                        @Param("schoolId") String schoolId,
                                                        @Param("subjectsId") String subjectsId,
                                                        @Param("studentName") String studentName,
                                                        @Param("startDate") String startDate,
                                                        @Param("endDate") String endDate,
                                                        @Param("returnType") String returnType,
                                                        @Param("status") String status,
                                                        @Param("adminId") String adminId
    );

    @Select({"<script>",
            "SELECT ",
            "t1.*, ",
            "t2.`name` as student_name, ",
            "t3.content, ",
            "getSubjectsName( t1.subjects_id ) as subjects_name, ",
            "t4.arrive_step, ",
            "t4.version_num, ",
            "t4.reason ",
            "FROM ",
            "audit_return_premium AS t1 ",
            "LEFT JOIN student AS t2 ON t1.student_id = t2.id ",
            "LEFT JOIN common_cause AS t3 ON t1.cause_id = t3.id AND t3.type = 1 AND t3.deleted = 0 ",
            "LEFT JOIN audit_claim AS t4 ON t1.id = t4.id",
            "WHERE t1.id = ${id} ",
            "GROUP BY t1.id ",
            "</script>"})
    AuditReturnPremium getAuditReturnPremiumDetails(@Param("id") Long id);

}
